package cn.ekuma.epos.datalogic.define.dao;

import java.util.List;

import cn.ekuma.data.dao.ViewDAO;

import com.openbravo.bean.AttributeInstInfo;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.PreparedSentence;
import com.openbravo.data.loader.SerializerWriteBasic;
import com.openbravo.data.loader.SerializerWriteString;
import com.openbravo.data.loader.Session;

public class AttributeInstInfoDAO extends ViewDAO<AttributeInstInfo> {
	I_Session sess;

	public AttributeInstInfoDAO(I_Session s) {
		super(s);
		this.sess = s;
	}

	@Override
	public Class getSuportClass() {
		return AttributeInstInfo.class;
	}

	@Override
	public AttributeInstInfo readValues(DataRead dr, AttributeInstInfo obj)
			throws BasicException {
		if (obj == null)
			obj = new AttributeInstInfo();
		obj.setAttid(dr.getString(1));
		obj.setAttname(dr.getString(2));
		obj.setId(dr.getString(3));
		obj.setValue(dr.getString(4));
		return obj;
	}

	public List<AttributeInstInfo> listAttributeInstInfo(String attsetId)
			throws BasicException {
		return new PreparedSentence(
				s,
				"SELECT A.ID, A.NAME, "
						+ sess.getDB().CHAR_NULL()
						+ ", "
						+ sess.getDB().CHAR_NULL()
						+ " "
						+ "FROM ATTRIBUTEUSE AU JOIN ATTRIBUTE A ON AU.ATTRIBUTE_ID = A.ID "
						+ "WHERE AU.ATTRIBUTESET_ID = ? "
						+ "ORDER BY AU.LINENO", SerializerWriteString.INSTANCE,
				this).list(attsetId);
	}

	public List<AttributeInstInfo> listAttributeInstInfo(String attsetId,
			String attsetinstid) throws BasicException {
		return new PreparedSentence(
				s,
				"SELECT A.ID, A.NAME, AI.ID, AI.VALUE "
						+ "FROM ATTRIBUTEUSE AU JOIN ATTRIBUTE A ON AU.ATTRIBUTE_ID = A.ID LEFT OUTER JOIN ATTRIBUTEINSTANCE AI ON AI.ATTRIBUTE_ID = A.ID "
						+ "WHERE AU.ATTRIBUTESET_ID = ? AND AI.ATTRIBUTESETINSTANCE_ID = ?"
						+ "ORDER BY AU.LINENO", new SerializerWriteBasic(
						Datas.STRING, Datas.STRING), this).list(new String[] {
				attsetId, attsetinstid });
	}

}
